package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.PageResult;
import com.itheima.constant.QueryPageBean;
import com.itheima.constant.Result;
import com.itheima.domain.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/10/15 11:17
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    //查询全部
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> list = roleService.findAll();
            return new Result(true, "查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }

    //添加数据;
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.add(role, permissionIds, menuIds);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    //删除数据;
    @RequestMapping("/deleteByid")
    public Result deleteByid(Integer id) {
        try {
            roleService.deleteByid(id);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            return new Result(false, "删除失败");
        }
    }

    //根据ID查询,页面回显;
    @RequestMapping("/findByid")
    public Result findByid(Integer id) {
        try {
            Role role = roleService.findByid(id);
            return new Result(true, "查询成功", role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    @RequestMapping("/getPermissionIds")
    public Result getPermissionIds(Integer id) {
        try {
            List<Integer> list = roleService.getPermissionIds(id);
            return new Result(true, "获取成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取失败");
        }
    }

    @RequestMapping("/getMenuIds")
    public Result getMenuIds(Integer id) {
        try {
            List<Integer> list = roleService.getMenuIds(id);
            return new Result(true, "获取成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取失败");
        }
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.update(role, permissionIds, menuIds);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

}
